package com.hoshiibuy.es.repository.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.es.entity.Student;
import com.hoshiibuy.es.repository.StudentRepository;
import com.hoshiibuy.es.util.JestClientUtil;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

@Repository("studentDao")
public class StudentRepositoryImpl implements StudentRepository {
    //es 索引名称
    private static final String ES_INDEX_NAME = "student_index";
    //es type名称
    private static final String ES_TYPE_NAME = "student_type";

    @Autowired
    private JestClientUtil jestClientUtil;

    //向索引中添加学生信息
    @Override
    public void addStudent(Student student) {
        Index index = new Index.Builder(student).index(this.ES_INDEX_NAME).
                type(this.ES_TYPE_NAME).build();

        try {
            //获取jestClient实例并向索引插入学生信息
            JestResult jestResult = jestClientUtil.createJestClient().execute(index);

            System.out.println(jestResult.isSucceeded());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //批量添加学生信息
    @Override
    public void batchAddStudent(List<Student> students) {
        if (!CollectionUtils.isEmpty(students)) {
            try {
                Bulk.Builder bulk = new Bulk.Builder().defaultIndex(this.ES_INDEX_NAME).
                        defaultType(this.ES_TYPE_NAME);
                //将学生列表信息循环假如bulk桶中
                for (Student student : students) {
                    Index index = new Index.Builder(student).build();
                    bulk.addAction(index);
                }
                //获取jestClient实例并向index中插入学生信息
                jestClientUtil.createJestClient().execute(bulk.build());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

    //根据学生姓名查询学生信息
    @Override
    public List<Student> queryStudentByName(String studentName) {
        //实例化SearchSourceBuilder对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //组装index 查询条件
        searchSourceBuilder.query(QueryBuilders.matchQuery("name", studentName));


        //利用SearchSourceBuilder对象构建index查询对象
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(this.ES_INDEX_NAME).addType(this.ES_TYPE_NAME).build();
        try {
            //获取jestClient实例并向对index执行查询语句 
            JestResult result = jestClientUtil.createJestClient().execute(search);
            //获取查询结果
            return result.getSourceAsObjectList(Student.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //创建es索引
    @Override
    public void createIndex() {
        try {
            jestClientUtil.createJestClient().execute(new CreateIndex.Builder(this.ES_INDEX_NAME).build());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public void page(Page<Student> pageObj, String index, String type, String sort){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.from((int)((pageObj.getCurrent()-1)*pageObj.getSize()));
        searchSourceBuilder.size((int)pageObj.getSize());
        searchSourceBuilder.sort(sort);
        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(index).addType(type).build();
        try {
            jestClientUtil.createJestClient().execute(search);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}